<?php
namespace App\Services\Core\Store\Moxie;
use Requests;

class MoxieService {

    public static function get($url, $apiName){


        $headers = array(
            'Content-Type' => 'application/json',
            'Authorization' => 'token ' . MoxieConfig::MOXIE_WEBHOOK_TOKEN);

        $return = Requests::get($url, $headers);
        $status = $return->status_code;
        if($status == 200){
            return $return->body;
        } else {
            self::error($status, $return->body, $apiName);
        }

        return null;
    }

    private static function error($status, $body, $apiName){

        $bodyObj = json_decode($body, true);
        if(array_key_exists("message", $bodyObj)){
            echo sprintf("%s, status:%s, message:%s\n", $apiName, $status, $bodyObj['message']);
        }else if(array_key_exists("detail", $bodyObj)){
            echo sprintf("%s, status:%s, detail:%s\n", $apiName, $status, $bodyObj['detail']);
        }
    }



    public static function operatorSend($params)
    {

        $resp = self::_crulPost($params, MoxieConfig::MOXIE_BILL_LOGIN);
        return$resp;
    }




         static  function _crulPost($postData, $url = '')
    {/*{{{*/
        if (empty($url)) {
            //Yii::log('openapi curl post数据时，目标url为空','error');
            return false;
        }
//        dd($postData);
        $postData = json_encode($postData);
        try {

            $headers = array(
                'Content-Type: application/json',
                'Authorization: apikey 3846f6f1e99e4f1b958111fda257c368'
            );

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            $res = curl_exec($curl);


            $errno = curl_errno($curl);
            $curlInfo = curl_getinfo($curl);
            $errInfo = 'curl_err_detail: ' . curl_error($curl);
            $errInfo .= ' curlInfo:' . json_encode($curlInfo);

            $arrRet = json_decode($res, true);

            //统一记录日志
            $logLevel = 'info';
            if (!is_array($arrRet) ) {
                $logLevel = 'warning';
            }
            curl_close($curl);
        } catch (Exception $e) {
//            print_r($e->getMessage());
        }

        //Yii::log("openapi curl post url: \t $url \t post: \t " . json_encode($postData) . " \t errno: $errno return: $res " . $errInfo, $logLevel);



        return $arrRet;
    }/*}}}*/



    //curl get请求
    public static  function _crulGet($headers,$url = '')
    {


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_SSLVERSION, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        //执行命令fd
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        $data = json_decode($data, true);
        return $data;
    }





}
